﻿@model ProductSummaryModel

@using SmartStore.Services.Localization;
@using SmartStore.Core.Domain.Orders;
@using SmartStore.Web.Models.Catalog;

@{
	Layout = "_Layout";
	Html.AddTitleParts(T("PageTitle.CompareProducts").Text);

	var specificationAttributes = new List<ProductSpecificationModel>();
	foreach (var product in Model.Items)
	{
		foreach (var attribute in product.SpecificationAttributes)
		{
			if (specificationAttributes.Where(sa => sa.SpecificationAttributeId == attribute.SpecificationAttributeId).FirstOrDefault() == null)
			{
				specificationAttributes.Add(attribute);
			}
		}
	}

	var tableWidth = (Model.Items.Count + 1) * 20;

	var hasRatings = Model.Items.Any(x => x.TotalReviews > 0);
	var hasBrands = Model.ShowBrand && Model.Items.Any(x => x.Manufacturer != null);
	var hasSkus = Model.ShowSku && Model.Items.Any(x => x.Sku.HasValue());
	var hasDescriptions = Model.ShowDescription && Model.Items.Any(x => x.ShortDescription.Value.HasValue());
	var hasDimensions = Model.Items.Any(x => x.Dimensions.HasValue());
	var hasWeight = Model.Items.Any(x => x.Weight.HasValue());
}

<div class="page compare-products-page">
	<div class="row mb-4 align-items-center">
		<div class="col">
			<div class="heading">
				<h1 class="heading-title fs-h1">@T("Products.Compare.Title")</h1>
			</div>
		</div>
		@if (Model.Items.Any())
		{
			<div class="col col-auto">
				<a class="btn btn-danger mb-2" href='@Url.Action("ClearCompareList")' rel="nofollow">
					<i class="far fa-trash-alt"></i>
					<span>@T("Products.Compare.Clear")</span>
				</a>
			</div>
		}
	</div>

    <div class="page-body">
        @if (Model.Items.Any())
        {
			<div class="table-responsive">
				<table class="table table-bordered table-striped table-compare" style="width: @(tableWidth)%">
					<thead>
						<tr>
							<th class="border-bottom-0 p-0">&nbsp;</th>
							@foreach (var product in Model.Items)
							{
								var showAddToCart = !product.Price.DisableBuyButton && !product.Price.CallForPrice && product.Parent.BuyEnabled;
								var showWishlist = !product.Price.DisableWishlistButton && product.Parent.WishlistEnabled;

								<th class="border-bottom-0 p-0">
									<div class="row xs-gutters flex-nowrap">
										@if (showAddToCart || showWishlist)
										{
											<div class="col-auto">
												<div class="btn-group">
													@if (showAddToCart)
													{
														<a data-href='@Url.Action("AddProductSimple", "ShoppingCart", new { productId = product.Id, shoppingCartTypeId = (int)ShoppingCartType.ShoppingCart })'
														   href="#"
														   class="btn btn-secondary btn-icon btn-flat btn-no-border ajax-cart-link rounded-0"
														   title="@T("ShoppingCart.AddToCart")"
														   rel="nofollow"
														   data-type="cart"
														   data-action="add">
															<i class="fa fa-cart-arrow-down"></i>
														</a>
													}
													@if (showWishlist)
													{
														<a data-href='@Url.Action("AddProductSimple", "ShoppingCart", new { productId = product.Id, shoppingCartTypeId = (int)ShoppingCartType.Wishlist })'
														   href="#"
														   class="btn btn-secondary btn-icon btn-flat btn-no-border ajax-cart-link rounded-0"
														   title="@T("ShoppingCart.AddToWishlist")"
														   rel="nofollow"
														   data-type="wishlist"
														   data-action="add">
															<i class="fal fa-heart"></i>
														</a>
													}
												</div>
											</div>
										}
										<div class="col text-right">
											<a class="btn btn-secondary btn-to-danger btn-flat btn-no-border btn-remove rounded-0" href='@Url.Action("RemoveProductFromCompare", new { id = product.Id })' rel="nofollow">
												@T("Common.Remove")
											</a>
										</div>
									</div>
								</th>
							}
						</tr>
						<tr>
							<th class="border-top-0 border-bottom-0 pb-1">&nbsp;</th>
							@foreach (var product in Model.Items)
							{
								<th class="border-top-0 border-bottom-0 pb-1">
									@if (product.Picture != null && product.Picture.ImageUrl.HasValue())
									{
										<a class="d-block text-center" href="@Url.RouteUrl("Product", new { SeName = product.SeName })">
											<img class="img-fluid" alt="@product.Picture.AlternateText" src="@product.Picture.ImageUrl" title="@product.Picture.Title" />
										</a>
									}
								</th>
							}
						</tr>
						@if (hasRatings)
						{
							<tr>
								<th class="border-top-0 border-bottom-0 py-1">&nbsp;</th>
								@foreach (var product in Model.Items)
								{
									<th class="border-top-0 border-bottom-0 py-1">
										@{ Html.RenderPartial("Product.List.Item.Rating", product); }
									</th>
								}
							</tr>
						}
						<tr>
							<th class="border-top-0 pt-1">&nbsp;</th>
							@foreach (var product in Model.Items)
							{
								<td class="border-top-0 pt-1">
									<a @Html.LanguageAttributes(product.Name) class="font-weight-medium" href="@Url.RouteUrl("Product", new { SeName = product.SeName })">@product.Name</a>
								</td>
							}
						</tr>
					</thead>
					<tbody>
						<tr>
							<th class="caption">&nbsp;</th>
							@foreach (var product in Model.Items)
							{
								<td>@{ Html.RenderPartial("Product.List.Item.Price", product); }</td>
							}
						</tr>
						@if (hasBrands)
						{
							<tr>
								<th class="caption">@T("Products.Manufacturer")</th>
								@foreach (var product in Model.Items)
								{
									<td>
										@if (product.Manufacturer != null)
										{
											<span @Html.LanguageAttributes(product.Manufacturer.Name)>@product.Manufacturer.Name</span>
										}
									</td>
								}
							</tr>
						}
						@if (hasSkus)
						{
							<tr>
								<th class="caption">@T("Products.Sku")</th>
								@foreach (var product in Model.Items)
								{
									<td>@product.Sku</td>
								}
							</tr>
						}
						@if (hasWeight)
						{
							<tr>
								<th class="caption">@T("Products.Weight")</th>
								@foreach (var product in Model.Items)
								{
									<td><span class="lrm">@product.Weight</span></td>
								}
							</tr>
						}
						@if (hasDimensions)
						{
							<tr>
								<th class="caption">@T("Products.Dimensions")</th>
								@foreach (var product in Model.Items)
								{
									<td><span class="lrm">@Html.Raw(product.Dimensions)</span></td>
								}
							</tr>
						}
						@if (hasDescriptions)
						{
							<tr>
								<th class="caption">@T("Products.Compare.ShortDescription")</th>
								@foreach (var product in Model.Items)
								{
									<td>
										@Html.Raw(product.ShortDescription)
									</td>
								}
							</tr>
						}
						@foreach (var specificationAttribute in specificationAttributes)
						{
							<tr>
								<th class="caption"><span @Html.LanguageAttributes(specificationAttribute.SpecificationAttributeName)>@specificationAttribute.SpecificationAttributeName</span></th>
								@foreach (var product in Model.Items)
								{
									var foundProductSpecs = product.SpecificationAttributes
                                        .Where(psa => psa.SpecificationAttributeId == specificationAttribute.SpecificationAttributeId);

									<td>
                                        @foreach (var spec in foundProductSpecs)
                                        {
                                            var specValue = spec != null ? spec.SpecificationAttributeOption : null;

                                            if (specValue != null && specValue.Value.HasValue())
                                            {
											    <div @Html.LanguageAttributes(specValue)>@specValue</div>
										    }
										    else
										    {
											    <div>&nbsp;</div>
										    }
                                        }
									</td>
								}
							</tr>
						}
						@if (Model.ShowFullDescription)
						{
							<tr>
								<th class="caption">@T("Products.Compare.FullDescription")</th>
								@foreach (var product in Model.Items)
								{
									<td @Html.LanguageAttributes(product.FullDescription) class="html-editor-content">
										@Html.Raw(product.FullDescription)
									</td>
								}
							</tr>
						}

						@{ Html.RenderWidget("compareproducts_row"); }
					</tbody>
				</table>
			</div>
        }
        else
        {
			<div class="alert alert-warning fade show">
				@T("Products.Compare.ListIsEmpty")
			</div>
        }
    </div>
</div>
